﻿using System;
using System.Windows;
using System.Windows.Forms;
using SGLL.Properties;

namespace SGLL
{
    public partial class FormSettings : FormTemplate
    {
        //Global Variables
        string id;
        //Class Constructor
        public FormSettings()
        {
            InitializeComponent();
        }
        //Form
        private void FormSettings_Load(object sender, EventArgs e)
        {
            
            textBoxEmail.Text = Properties.Settings.Default.Email;
            textBoxPassword.Text = Properties.Settings.Default.Password;
            checkBoxToolTipText.Checked = Properties.Settings.Default.ShowToolTips;
            toolTip.Active = Properties.Settings.Default.ShowToolTips;
            refreshTypesOfSamples();
        }
        //TextBoxes
        private void textBoxs_TextChanged(object sender, EventArgs e)
        {
            EnableButtonSave();
        }
        private void textBoxTypeOfSample_TextChanged(object sender, EventArgs e)
        {
            buttonSubmit.Enabled = checkBoxIsActive.Enabled = checkBoxIsActive.Checked = !string.IsNullOrWhiteSpace(textBoxTypeOfSample.Text);
        }
        private void textBoxTypeOfSample_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !ClassFunctions.TextInputFilter(ClassFunctions.Filters.Alphanumeric, e.KeyChar);
        }
        private void typeOfSample_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = buttonSubmit;
        }
        //Tool Strip Button
        protected override void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                Properties.Settings.Default.Email = textBoxEmail.Text;
                Properties.Settings.Default.Password = textBoxPassword.Text;
                Properties.Settings.Default.ShowToolTips = checkBoxToolTipText.Checked;
                Properties.Settings.Default.Save();
                //Update user
                ((FormMain)this.MdiParent).toolStripStatusLabelMessage.Text = Properties.Resources.toolStripStatusSavedSettings;
            }
            catch (Exception exception)
            {
                ClassFunctions.ErrorLog(exception);
            }
        }
        protected override void toolStripButtonClear_Click(object sender, EventArgs e)
        {
            textBoxEmail.Text = textBoxPassword.Text = string.Empty;
        }
        protected override void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            FormMain formMain = (FormMain)this.ParentForm;
            formMain.toolStripMenuItemConfiguration.Enabled = true;
            Close();
        }
        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //Validations
                if (string.IsNullOrWhiteSpace(id) && ClassFunctions.CheckIfExists(string.Format(ClassSQL.GetTypesOfSamplesByDescription, textBoxTypeOfSample.Text.Trim()))) throw new Exception(string.Format(Resources.ErrorDuplicate, textBoxTypeOfSample.Text));
                //Start
                //Variables
                //Code
                ClassFunctions.SetData(ClassSQL.SetUpTypesOfSamples, (string[]) new object[] {id, textBoxTypeOfSample.Text.Trim(), checkBoxIsActive.Checked ? 1 : 0 });
                refreshTypesOfSamples();
                clearTypesOfSamples();
                //Let the user know
                var parentForm = (FormMain) ParentForm;
                if (parentForm != null)
                    parentForm.toolStripStatusLabelMessage.Text = Resources.SuccessfulSave;
                //Log File
            }
            catch (Exception exception)
            {
                //Report Error
                MessageBox.Show(string.Format(Resources.ErrorDuplicate, textBoxTypeOfSample.Text), Resources.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                ((FormMain)this.ParentForm).toolStripStatusLabelMessage.Text = exception.Message;
                textBoxTypeOfSample.Focus();
                textBoxTypeOfSample.SelectAll();
            }
        }
        //DataGridView
        private void dataGridViewTypesOfSamples_RowEnter(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            try
            {
                //Validaciones
                //Inicio
                //Variables
                //Código
                id = dataGridViewTypesOfSamples.Rows[e.RowIndex].Cells[0].Value.ToString();
                textBoxTypeOfSample.Text = dataGridViewTypesOfSamples.Rows[e.RowIndex].Cells[1].Value.ToString();
                checkBoxIsActive.Checked = dataGridViewTypesOfSamples.Rows[e.RowIndex].Cells[2].Value.Equals("Activo");
                textBoxTypeOfSample.SelectAll();
                textBoxTypeOfSample.Focus();
                //Informar al Usuario
                //Log File
            }
            catch (Exception exception)
            {
                //Reporte de Error
                ClassFunctions.ErrorLog(exception);
            }
            finally
            {
                //Fin
            }
        }
        //General Purpose-Functions
        private void EnableButtonSave()
        {
            toolStripButtonSave.Enabled = !string.IsNullOrWhiteSpace(textBoxEmail.Text) && !string.IsNullOrWhiteSpace(textBoxPassword.Text);
        }
        private void refreshTypesOfSamples()
        {
            dataGridViewTypesOfSamples.RowEnter -= dataGridViewTypesOfSamples_RowEnter;
            this.tableAdapterTypesOfSamples.Fill(this.dataSetTypeOfSamples.PROCEDURE_SELECT_TYPES_OF_SAMPLES);
            dataGridViewTypesOfSamples.ClearSelection();
            dataGridViewTypesOfSamples.RowEnter += dataGridViewTypesOfSamples_RowEnter;
        }
        private void clearTypesOfSamples()
        {
            textBoxTypeOfSample.Text = string.Empty;
            id = null;
            checkBoxIsActive.Checked = false;
            textBoxTypeOfSample.Focus();
        }
    }
}